﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using GomMinhLongBL;
using GomMinhLongDO;
using System.Text;
using System.Text.RegularExpressions;

namespace GomMinhLong
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.showpopup();
            if (!IsPostBack)
            {
                string CurrentUrl = "http://" + Request.Url.Host + Request.RawUrl;
                hplFacebook.NavigateUrl = hplFacebook.NavigateUrl + CurrentUrl;
                Session["popup"] = "0";
            }
            this.ActiveMenu();
            this.LinkMenu();
            this.BindDataRpt();

            if (Session["GioHang"] != null)
            {
                DataTable dtgh = (DataTable)Session["GioHang"];
                hplGioHang.Text = dtgh.Rows.Count.ToString() + " Sản phẩm ";
            }

            else
            {
                hplGioHang.Text = "0 Sản phẩm ";
            }
            lblQuangCaoPhai.Text = new sysConfigBL().Select(new sysConfigDO() { ConfigID = 17 }).ConfigValue;
            lblQuangCaoTrai.Text = new sysConfigBL().Select(new sysConfigDO() { ConfigID = 16 }).ConfigValue;
            
        }

        public void ActiveMenu()
        {
            string P = "";
            P = Request.QueryString["Page"];
            if (P == "Trang-chu")
            {
                hplHomePage.CssClass = "active";
                hplHomePageBottom.CssClass = "active";
            }
            else if (P == "Gioi-Thieu")
            {
                hplIntroduction.CssClass = "active";
                hplIntroductionBottom.CssClass = "active";
            }
            else if (P == "Dich-Vu")
            {
                hplServices.CssClass = "active";
                hplServicesBottom.CssClass = "active";
            }
            else if (P == "Tin-Tuc")
            {
                hplNewsBottom.CssClass = "active";
                hplHomePage.CssClass = "active";
            }
            else if (P == "San-Pham")
            {
                hplProductionBottom.CssClass = "active";
                hplHomePage.CssClass = "active";
            }
            else if (P == "Lien-He")
            {
                hplContact.CssClass = "active";
                hplContactBottom.CssClass = "active";
            }
            else if (P == "Khuyen-Mai")
            {
                hplPromotionBottom.CssClass = "active";
                hplHomePage.CssClass = "active";
            }
            else if (P == "Chi-Tiet-Khuyen-Mai")
            {
                hplPromotionBottom.CssClass = "active";
                hplHomePage.CssClass = "active";
            }
            else if (P == "Chi-Tiet-Tin-Tuc")
            {
                hplNewsBottom.CssClass = "active";
                hplHomePage.CssClass = "active";
            }
            else if (P == "Chi-Tiet-San-Pham")
            {
                hplProductionBottom.CssClass = "active";
                hplHomePage.CssClass = "active";
            }
            else
            {
                hplHomePage.CssClass = "active";
                hplHomePageBottom.CssClass = "active";
            }
        }

        public void LinkMenu()
        {
            hplHomePage.NavigateUrl = "/Trang-chu.htm";
            hplIntroduction.NavigateUrl = "/Gioi-Thieu/1/Gioi-Thieu.htm";
            hplServices.NavigateUrl = "/Dich-Vu/3/Dich-Vu.htm";
            hplContact.NavigateUrl = "/Lien-He.htm";
            hplHomePageBottom.NavigateUrl = "/Trang-chu.htm";
            hplIntroductionBottom.NavigateUrl = "/Gioi-Thieu/1/Gioi-Thieu.htm";
            hplProductionBottom.NavigateUrl = "/San-Pham/All/San-Pham.htm";
            hplServicesBottom.NavigateUrl = "/Dich-Vu/3/Dich-Vu.htm";
            hplNewsBottom.NavigateUrl = "/Tin-Tuc/4/Tin-Tuc.htm";
            hplPromotionBottom.NavigateUrl = "/Khuyen-Mai/6/Khuyen-Mai.htm";
            hplContactBottom.NavigateUrl = "/Lien-He.htm";
        }

        public void BindDataRpt()
        {
            DataTable dtIntroduction = new DataTable("dtIntroduction");
            dtIntroduction = new cmsCategoryBL().SelectParentId(1);
            if ((dtIntroduction != null) && (dtIntroduction.Rows.Count > 0))
            {
                rptIntroduction.DataSource = dtIntroduction;
                rptIntroduction.DataBind();
            }

            DataTable dtServices = new DataTable("dtServices");
            dtServices = new cmsCategoryBL().SelectParentId(3);
            if ((dtServices != null) && (dtServices.Rows.Count > 0))
            {
                rptServices.DataSource = dtServices;
                rptServices.DataBind();
            }

            DataTable dtNews = new DataTable("dtNews");
            dtNews = new cmsCategoryBL().SelectParenAndChid(4);
            if ((dtNews != null) && (dtNews.Rows.Count > 0))
            {
                rptNews.DataSource = dtNews;
                rptNews.DataBind();
            }

            DataTable dtProduct = new DataTable("dtProduct");
            dtProduct = new cmsCategoryBL().SelectParentId(2);
            if ((dtProduct != null) && (dtProduct.Rows.Count > 0))
            {
                rptProduct.DataSource = dtProduct;
                rptProduct.DataBind();
            }

            DataTable dtQuangCao = new DataTable("dtQuangCao");
            dtQuangCao = new cmsDoiTacBL().SelectAllTopN(4);
            if ((dtQuangCao != null) && (dtQuangCao.Rows.Count > 0))
            {
                rptQuangCao.DataSource = dtQuangCao;
                rptQuangCao.DataBind();
            }

            DataTable dtSlide = new DataTable("dtSlide");
            dtSlide = new SlideBL().SelectAll();
            if ((dtSlide != null) && (dtSlide.Rows.Count > 0))
            {
                rptSlide.DataSource = dtSlide;
                rptSlide.DataBind();
            }
        }

        public string Change_AV(string ip_str_change)
        {
            if (string.IsNullOrEmpty(ip_str_change)) return "default.htm";
            Regex v_reg_regex = new Regex("\\p{IsCombiningDiacriticalMarks}+");
            string v_str_FormD = ip_str_change.Normalize(NormalizationForm.FormD);
            string rt = v_reg_regex.Replace(v_str_FormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D').Replace(" ", "-").Replace("/", "-");
            rt = rt.Replace("\\", "-").Replace("'", "-").Replace(":", "-");
            rt = rt + ".htm";
            return rt;

        }

        protected void rptProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemIndex < 0)
            {
                return;
            }
            DataRowView dtItem = (DataRowView)e.Item.DataItem;
            Repeater rptProductItem = (Repeater)e.Item.FindControl("rptProductItem");
            Panel pnlProductItem = (Panel)e.Item.FindControl("pnlProductItem");
            //HyperLink hplProductItem = (HyperLink)e.Item.FindControl("hplProductItem");

            //if (hplProductItem != null)
            //{
            //    hplProductItem.NavigateUrl = "/San-Pham/" + dtItem["CategoryID"].ToString() + "/" + Change_AV(dtItem["Title"].ToString());
            //    hplProductItem.Text = dtItem["Title"].ToString();
            //    hplProductItem.ToolTip = dtItem["Title"].ToString();
                
            //}

            int ProductId = 0;
            ProductId = int.Parse(dtItem["CategoryID"].ToString());
            if (rptProductItem != null)
            {
                if (ProductId > 0)
                {
                    DataTable dtProductItem = new DataTable("dtProductItem");
                    dtProductItem = new cmsCategoryBL().SelectParentId(ProductId);
                    if ((dtProductItem != null) && (dtProductItem.Rows.Count > 0))
                    {
                        rptProductItem.DataSource = dtProductItem;
                        rptProductItem.DataBind();
                    }
                }
            }
            if (pnlProductItem != null)
            {
                string IdUrl = "";
                string PageUrl = "";
                if (Request.QueryString["ID"] != null)
                {
                    IdUrl = Request.QueryString["ID"];
                }
                if (Request.QueryString["Page"] != null)
                {
                    PageUrl = Request.QueryString["Page"];
                }
                if (PageUrl == "San-Pham")
                {
                    string IdUrlRoot = CategoryRoot(IdUrl);
                    string CategoryIDRoot = CategoryRoot(dtItem["CategoryID"].ToString());
                    if ( IdUrlRoot== CategoryIDRoot)
                    {
                        pnlProductItem.Visible = true;
                    }
                    else
                    {
                        pnlProductItem.Visible = false;
                    }
                }
                else if (PageUrl == "Chi-Tiet-San-Pham")
                {
                    string sCategoryID = new cmsSanPhamBL().Select(new cmsSanPhamDO() { SanPhamID = int.Parse(IdUrl) }).LoaiSanPhamID.ToString();
                    string IdUrlRoot = CategoryRoot(sCategoryID);
                    string CategoryIDRoot = CategoryRoot(dtItem["CategoryID"].ToString());
                    if (IdUrlRoot == CategoryIDRoot)
                    {
                        pnlProductItem.Visible = true;
                    }
                    else
                    {
                        pnlProductItem.Visible = false;
                    }
                }
                else
                {
                    pnlProductItem.Visible = false;
                }
            }
        }

        public string CategoryRoot(string CategoryId)
        {
            string ParentID = new cmsCategoryBL().Select(new cmsCategoryDO() { CategoryID = int.Parse(CategoryId) }).ParentID.ToString();
            if (ParentID != "2")
            {
                return CategoryRoot(ParentID);
            }
            else
            {
                return CategoryId;
            }
        }

        private void showpopup()
        {
            string popup = new sysConfigBL().Select(new sysConfigDO { ConfigID = 18 }).ConfigValue.ToString();
            int pu = int.Parse(popup);

            if (pu == 0)
            {
                pnlBacnkgroundHomepage.Visible = false;
            }
            else
            {
                if ((Session["popup"] != null) && (Session["popup"].ToString() == "0"))
                {
                    ucPopupHomePage1.Visible = false;
                    pnlBacnkgroundHomepage.Visible = false;
                }
                else
                {
                    ucPopupHomePage1.Visible = true;
                    pnlBacnkgroundHomepage.Visible = true;
                }
            }
        }
    }
}
